#ifndef VIEWPOINT_H
#define VIEWPOINT_H

#pragma once
#include "modelPoint.h"
#include "bllLine.h"

class viewPoint
{
private:
	viewPoint(CWnd* wnd);
public:
	static viewPoint* inst()
	{
		if(m_inst)
		{
			return m_inst;
		}
		else
		{
			m_inst = new viewPoint(AfxGetMainWnd());
			return m_inst;
		}
	}
	static void release(){DELETE(m_inst);}

	void setBtnDown(POINT& pt)
	{
		bool bCross = false;
		if(m_data.getData()[m_data.getData().size()-1].size()>0)
		{
			bllLine line1(*(m_data.getData()[m_data.getData().size()-1].rbegin()),pt);
			for (vector<list<POINT>>::iterator it=m_data.getData().begin();it!=m_data.getData().end();it++)
			{
				for (list<POINT>::iterator itList=(*it).begin();itList!=(*it).end();itList++)
				{
					list<POINT>::iterator temp = itList;
					++itList;
					if(itList!=(*it).end())
					{
						bCross = line1.isCross(bllLine(*temp,*itList));
					}
					--itList;
				}
			}
		}
		if(!bCross)
		{
			m_data.addPoint(pt);
			m_BtnDown = true;
			Update();
		}
	}
	void setBtnUp()
	{
		m_BtnDown = false;
		Update();
	}
	bool isBtnDown(){return m_BtnDown;}

	void drawLine(POINT& s);
	void drawShape();
	void Update()
	{
		if(m_data.getData()[m_data.getData().size()-1].size()==1)
		{
			m_window->Invalidate(FALSE);
		}
		else
		{
			m_window->InvalidateRect(m_data.getUpdateRect(),FALSE);
		}
	}
	void next(){m_data.addShape();}

private:
	static viewPoint* m_inst;
	bool m_BtnDown;
	CDC* m_pDc;
	CWnd* m_window;
	bllShape* m_pShape;
	modelPoint m_data;
	list<CGdiObject*> m_gdiObj;
};

#endif